package com.zhang.second.day02;

import com.zhang.second.utils.ClickEvent;
import com.zhang.second.utils.ClickSource;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @title: filter 算子
 * @author: zhang
 * @date: 2022/1/29 19:45
 */
public class Example3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .filter(new FilterFunction<ClickEvent>() {
                    @Override
                    public boolean filter(ClickEvent value) throws Exception {
                        return value.username.equals("Mary");
                    }
                })
                .print("filter");

        //使用flatmap实现filter
        env
                .addSource(new ClickSource())
                .flatMap(new FlatMapFunction<ClickEvent, ClickEvent>() {
                    @Override
                    public void flatMap(ClickEvent value, Collector<ClickEvent> out) throws Exception {
                        if (value.username.equals("Mary")) {
                            out.collect(value);
                        }
                    }
                })
                .print("flatmap");

        env.execute();
    }
}
